<?php
namespace App\Admin\Controllers\Order;

use App\Admin\Ajax\Order\AmazonAjax;
use App\Models\Amazon\AmazonOrder;
use App\Models\Amazon\AmazonOrderOld;
use App\Models\CustomerDelivery\CustomerDelivery;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
class AmazonController extends AdminController{

    protected $title = '亚马逊订单查询';

    protected $filters = [
        'amazon_order_id' => null
    ];

    protected function grid()
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);

        //$querySql = $query->toSql();
        //dd($querySql);
        //$list = DB::table(DB::raw("($querySql) as a"))->mergeBindings($query);

        $paginator = $this->_paginator($query);
        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['amazon_order_id'] = $this->filters['amazon_order_id'];
        return view('order.amazon.index', compact('paginator','filtersBlade','action'));
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){

        $query = AmazonOrder::query();
        foreach ($filters as $field => $filter){
            if(!$filter)continue;
            //联合查询
            if($field == 'amazon_order_id'){
                $amazon_order_ids = array_slice(explode(',',$filter),0,10);

                //ordrs_new
                $ordersSql = $query
                    ->whereIn('amazon_order_id', $amazon_order_ids)
                    ->select('id', 'amazon_order_id', 'purchase_date', 'marketplace_id', 'sales_channel', 'order_status', 'created_at', 'updated_at');
                //ordrs_old
                $query = AmazonOrderOld::query()
                    ->whereIn('amazon_order_id', $amazon_order_ids)
                    ->select('id', 'amazon_order_id', 'purchase_date', 'marketplace_id', 'sales_channel', 'order_status', 'created_at', 'updated_at')
                    ->union($ordersSql);
            }
        }
        return $query;
    }

    private function _paginator($query){
        return $query->orderBy('id', 'DESC')->paginate((int) (request("per_page",10)))->appends(Request::input());
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');
        $ajax = new AmazonAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    public function modal(){
        $action = request('action');
        $param = request('param');
        $modal = new AmazonAjax();
        return $param ? call_user_func([$modal,$action],$param) : call_user_func([$modal,$action]);
    }

}
